import axios from "axios";
import { sessionStorageFn } from "@/utils/storage.js";
let http = axios.create({
  baseURL: import.meta.env.APP_BASE_API,
  // baseURL: "http://140.207.48.210:8500"
});

http.interceptors.request.use(
  (config) => {
    // if (localStorageFn.get('token')) {
    //   config.headers.Authorization = 'Bearer ' + localStorageFn.get('token')
    // }
    config.headers["tenant-id"] = "C0005";

    return config;
  },
  (error) => {
    console.log(error);
  }
);

http.interceptors.response.use(
  ({ data }) => {
    if (data.code) {
      if (data.code === 200) {
        return data.data;
      } else {
        if (data.code === 401) {
          // localStorage.removeItem('token')
          return Promise.reject(data.message);
        } else {
          return Promise.reject(data.message);
        }
      }
    } else {
      return data;
    }
  },
  (err) => {
    console.log(err);
  }
);

export { http };
